package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.tools.Constants;

public class C2S9wantBuy extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:" + Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	/**
	 * 
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb9] whitebox9 get begin");
		System.out.println("版本> 15");
		PrintWriter writer = response.getWriter();
		String all = null;
		String user_id = null;
		String place = null;
		String thing = null;
		String price = null;
		while (er.hasMoreElements()) {
			String element = er.nextElement();
			if (element.equals("all")) {
				all = request.getParameterValues("all")[0];
				System.out.println("[vswb9] all:" + all);
			}
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
				System.out.println("[vswb9] user_id:" + user_id);
			}
			if (element.equals("price")) {
				price = request.getParameterValues("price")[0];
				System.out.println("[vswb9] price:" + price);
			}
			if (element.equals("place")) {
				place = request.getParameterValues("place")[0];
				System.out.println("[vswb9] place:" + place);
			}
			if (element.equals("thing")) {
				thing = request.getParameterValues("thing")[0];
				System.out.println("[vswb9] thing:" + thing);
			}
		}
		
		if((all == null) && (user_id != null) &&(place != null) &&(thing != null) && (price != null)){
			DB.insert("insert into buy(user_id,place,thing,price,time) values('"+user_id+"','"+place+"','"+thing+"','"+price+"',now());");
		}
		if((all != null)){
			ResultSet rs = DB.select("select * from buy,user where buy.user_id=user._id order by time desc;");
			try {
				while(rs.next()){
					String nick_name = rs.getString("phone_number");
					String place1 = rs.getString("place");
					String thing1 = rs.getString("thing");
					String time1 = rs.getString("time");
					String price1 = rs.getString("price");
					String user_id1 = rs.getString("user_id");
					writer.println(nick_name+"莪"+place1+"峩"+thing1+"僫"+time1+"鄂"+price1+ "呝"+user_id1+"~");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
